<template>
  <div class="login-view">
    <van-nav-bar title="登录" left-text="返回" left-arrow @click-left="router.back()"/>
    <van-form @submit="login">
      <van-cell-group inset>
        <van-field v-model="username" label="账号" placeholder="请输入用户名" autocomplete="username"/>
        <van-field v-model="password" label="密码" placeholder="请输入密码" type="password" autocomplete="current-password" />
      </van-cell-group>
      <div style="margin: 16px;">
        <van-button round block type="primary" native-type="submit">
          提交
        </van-button>
      </div>
      <div style="margin: 16px;">
        <van-button round block type="primary" @click="router.push('/register')">
          没有账号？去注册
        </van-button>
      </div>
    </van-form>

  </div>
</template>

<script setup lang="ts">
import { showFailToast } from "vant";
import { getUserData } from "../api"
import { useBaseStore, useUserStore} from "../store";
const baseStore = useBaseStore()
const userStore = useUserStore();
const router = useRouter();
let username = ref("");
let password = ref("");

let login = async () => {
  let result = await getUserData({ username: username.value, password: password.value, status: "login" })
  console.log(result);
  if (result.data.code != 0) {
    showFailToast(`用户名或密码错误！`);
    username.value = "";
    password.value = "";
    return
  }
  showFailToast(`登录成功`);
  localStorage.setItem('token', result.data.user_id);
  baseStore.nowAct = 0;
  userStore.CartGoods = [];
  router.push({ path: "index/home" })
}

</script>

<style scoped></style>
